#include "info.h"
#include "ui_info.h"
#include "mywidget.h"


Info::Info(QWidget *parent):
    QDialog(parent),
    ui(new Ui::Info)
{
    ui->setupUi(this);
    setMinimumSize(660,450);
    setMaximumSize(660,450);
    setWindowTitle(tr("ZZU成绩查询系统"));

    this->netManager = new QNetworkAccessManager(this);
    QObject::connect(netManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
    ui->label->setText(tr("正在加载，请稍等…………"));
    startPost();
}

Info::~Info()
{
    delete ui;
}


//取当前学期成绩的SLOT   lastUpdateTime: 2013-7-26 17:28:39
 void Info::replyFinished(QNetworkReply *reply)
{
    QTextCodec *codec = QTextCodec::codecForName("gb2312");   //这里一定要用gb2312,它是ZZU网页编码格式
    QString t = codec->toUnicode((reply->readAll()));
    int i =t.indexOf("特别说明");
    QString t2=t.left(i);

    //返回其他学期成绩查询地址
     everyTermStr =t2;

    //取出当前学期,隐藏combox中高年级

     QString currentTerm =t2;
     int j=  currentTerm.count("学期<");  //j为public变量
     for(int i=7;i>j;i--)
     {
         ui->comboBox->removeItem(i);
     }


    //判断选择的查询类型,并执行相应操作
     myWidget *ptr = (myWidget *)parentWidget();
      QString select = ptr->select();

      if(select =="0")  //成绩查询
      {
         int m = t2.indexOf("打印时间");
         int n = t2.indexOf("退出系统");
         t2=t2.replace(m-43,n,"");
      //   qDebug()<<t2;

         this->ui->textBrowser->setText(t2);
         QString term;
         int qwer = t2.indexOf("&nbsp;学期:");
         int asdf = t2.mid(qwer+9,1).toInt();
          //qDebug()<<"@@@@@@@@@@@@@@"<<term;
         switch(asdf-1)
         {
             case 0: term ="大一上学期";break;
             case 1: term ="大一下学期";break;
             case 2: term ="大二上学期";break;
             case 3: term ="大二下学期";break;
             case 4: term ="大三上学期";break;
             case 5: term ="大三下学期";break;
             case 6: term ="大四上学期";break;
             case 7: term ="大四下学期";break;
         }
         ui->label->setStyleSheet("color:red;font-size:15px;");
         ui->label->setText(tr("加载成功，你的")+term+tr("成绩如下:"));
       //  qDebug()<<t2;

      }else if(select =="1")  //学生信息
      {

          //将图片下载到本地，并在TextBrowser显示,因为网页源码中的图片不是绝对地址,所以要保存到本地后显示
         int a=t2.indexOf("http://jw.zzu.edu.cn/scripts/stuinfo.dll/getphoto?userid");
         QString imageUrl = t2.mid(a,115);

         QNetworkAccessManager *manager = new QNetworkAccessManager(this);
         connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(getImageFinished(QNetworkReply*)));
         manager->get(QNetworkRequest(QUrl(imageUrl)));

         ui->comboBox->hide();
         ui->label->setText(tr("学生信息如下"));
         ui->label_2->hide();
         int m = imageUrl.count();
         int n = t2.indexOf(imageUrl);
         QString str="student.jpg";
         t2.replace(n,m,str);
         this->ui->textBrowser->setText(t2);

      }else if( (select == "2") | (select =="3"))  //选课系统 && 课表
      {
        ui->comboBox->hide();
        ui->label_2->hide();

        this->ui->textBrowser->setText(t2);
      }
}


//保存图片到本地的SLOT
void Info::getImageFinished(QNetworkReply *image)
{
    QPixmap *currentPicture = new QPixmap;
    currentPicture->loadFromData(image->readAll());
   // QDateTime now;
    //QString filename = now.currentDateTime().toString("yyMMddhhmmss.jpg");
    QString filename ="student.jpg";
    currentPicture->save(filename);//保存图片


}

//POST方法
void Info::do_post(QString url, QString _content)
{
    QByteArray content = _content.toAscii();
    int contentLength = content.length();
    QNetworkRequest req;
    req.setUrl(QUrl(url));
    req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    req.setHeader(QNetworkRequest::ContentLengthHeader,contentLength);
    this->netManager->post(req,content);
}

//开始提交
void Info::startPost()
{
    //post提交
    myWidget *ptr = (myWidget *)parentWidget();
    QString content = ptr->postString();
    QString url = ptr->currentSelect();

 //   qDebug()<<"#########"<<content;
    do_post(url,content);
}

//返回登录界面
void Info::on_pushButton_clicked()
{
    close();
    myWidget *ptr = (myWidget *)parentWidget();
    ptr->show();
}



//这里写取出每学期对应链接的方法
QString Info::currentTermLink(QString term)
{
    QString link,currLink_01,currLink_02;
    int j=everyTermStr.indexOf(term);
    link = everyTermStr.mid(j-60,58);  //link为"userid="后面的字符串
 //   qDebug()<<link;
    currLink_01 =everyTermStr.mid(j-6,5);
    currLink_02 =everyTermStr.mid(j-15,15);  //第1学期专用
  //  qDebug()<<"##########"<<currLink_02;

    if( (currLink_01 =="&nbsp" )| (currLink_02 == "<font size=\"2\">"))
    {
        return "error";
    }else{
        return link;
    }

}




//GET 获取页面内容到本地
void Info::do_get(QString url, QString content)
{
    url = url + content;
    QNetworkRequest req;
    req.setUrl(QUrl(url));
    this->netManager->get(req);
}

//显示其他学期成绩
void Info::ShowOtherTermScore()
{
    QString url;
    QString content;
    url = "http://jw.zzu.edu.cn/scripts/qscore.dll/search?userid=";
    content = currentTermLink(ui->comboBox->currentText());

    if (content =="error")
    {
        QMessageBox::warning(this,tr("警告"),tr("页面成绩已经是当前学期成绩!"));

    }else{
        do_get(url,content);
    }
}




void Info::on_comboBox_activated()
{
  //  ui->label->setText("正在加载，请稍等…………");
    ShowOtherTermScore();
}
